//
//  HomeTodayCollectView.m
//  Cat
//
//  Created by lushitong on 2018/12/22.
//  Copyright © 2018年 lushitong. All rights reserved.
//

#import "HomeTodayCollectView.h"
#import "SlideCollectionViewCell.h"
#import "HomePriorityInfo.h"
#import "DetailViewController.h"

@interface HomeTodayCollectView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) NSMutableArray *hotsMutArray;

@end

@implementation HomeTodayCollectView

- (void)setHotsArray:(NSArray *)hotsArray {
	_hotsArray = hotsArray;
	self.hotsMutArray = hotsArray.mutableCopy;
	[self reloadData];
}

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout{
	self = [super initWithFrame:frame collectionViewLayout:layout];
	if (self) {
		self.backgroundColor = [UIColor whiteColor];
		self.delegate = self;
		self.dataSource = self;
		self.clipsToBounds = YES;
		self.showsHorizontalScrollIndicator = NO;
		[self addGesture];
		[self registerClass:[SlideCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([SlideCollectionViewCell class])];
	}
	return self;
}

- (void)handleSwipeGesture:(UISwipeGestureRecognizer *)swipeGesture {
	
	UISwipeGestureRecognizer *gesture = swipeGesture;
	
	CGFloat itemWidth = 290.0;
	CGFloat margin = 10.0;
	NSInteger width = itemWidth+margin;
	
	// 计算当前滑动的cell的索引
	CGPoint point = [gesture locationInView:self];
	NSInteger index = (point.x-15) / width;
	NSLog(@"index:%ld x:%lf contentOffset:%lf",index,point.x,self.contentOffset.x);
	
	// 将点转化为window上的点，计算该点在当前屏幕上的x坐标
	UIWindow *window = [UIApplication sharedApplication].keyWindow;
	CGPoint windowPoint = [self convertPoint:point toView:window];
	NSInteger x = (NSInteger)windowPoint.x % (NSInteger)ScreenWidth;
	
	if (gesture.direction == UISwipeGestureRecognizerDirectionLeft){
		
		// 右划特殊考虑最后一个
		if (index <= self.hotsMutArray.count-2 && x < width+15){
			index = index + 1;
		}
		
		
	} else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionRight){
		
		// 左划特殊考虑第一个和倒数第二个
		if (index > 0){
			
			if (self.contentOffset.x > width*(self.hotsMutArray.count-2) && index == self.hotsMutArray.count -2) {
				NSLog(@"这是在最后一页左划倒数第二个item,index不变");
			} else {
				
				if (x <= 15){
					
				}else if ((x > 15 && x < width+15)||index == 1) {
					index = index - 1;
				} else {
					index = index - 2;
				}
			}
		}
	}
	
	if (index == 10-1) { // 往最后一个页面滑动
		
		[UIView animateWithDuration:0.3 animations:^{
			[self setContentOffset:CGPointMake(width*index-(ScreenWidth-15-104-15), 0) animated:YES];
		}];
	} else {
		
		[UIView animateWithDuration:0.3 animations:^{
			[self setContentOffset:CGPointMake(width*index, 0) animated:YES];
		}];
	}
}

- (void)addGesture {
	
	UISwipeGestureRecognizer * swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];
	swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
	[self addGestureRecognizer:swipeRight];
	
	UISwipeGestureRecognizer * swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];
	swipeRight.direction = UISwipeGestureRecognizerDirectionLeft;
	[self addGestureRecognizer:swipeLeft];
	
}

#pragma mark - collection view delegate

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
	return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
	return self.hotsMutArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
	SlideCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([SlideCollectionViewCell class]) forIndexPath:indexPath];
	HotsModel *model = [self.hotsMutArray safeObjectAtIndex:indexPath.row];
	[cell setModel:model];
	return cell;
}


#pragma mark - collection view data source
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
	HotsModel *mode = [self.hotsMutArray safeObjectAtIndex:indexPath.row];
	DetailViewController *de = [[DetailViewController alloc] init];
	de.proudctID = mode.productId;
	[self.currentNavigationController pushViewController:de animated:YES];
}

- (NSMutableArray *)hotsMutArray {
	if (!_hotsMutArray) {
		_hotsMutArray = [[NSMutableArray alloc] init];
	}
	return _hotsMutArray;
}


@end
